/*数据表格行编辑*/
	var editIndex1 = undefined;
	function endEditing(){
		if (editIndex1 == undefined){return true}
		if ($('#intoStorage').datagrid('validateRow', editIndex1)){
			$('#intoStorage').datagrid('endEdit', editIndex1);
				editIndex1 = undefined;
				return true;
		} else {
			return false;
		}
	}
	function onClickRow(index){
		if (editIndex1 != index){
			if (endEditing()){
				$('#intoStorage').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
					editIndex1 = index;
			} else {
				$('#intoStorage').datagrid('selectRow', editIndex1);
			}
		}
	}
/*数据表格行删除*/	
	function  go(val,row){
		return "<a href='#'  class='button-delete button-danger  l-btn l-btn-small' onclick='deleterow(this)' ><span class='l-btn-left' ><span class='l-btn-text'>删除</span></span></a>";
	} 
	function getRowIndex(target){
		var tr = $(target).closest('tr.datagrid-row');
		return parseInt(tr.attr('datagrid-row-index'));
	}
	function deleterow(target){
		$.messager.confirm('Confirm','确定删除？',function(r){
			if (r){
				$('#intoStorage').datagrid('deleteRow', getRowIndex(target));
			}
		});
	}
$(document).ready(function () {		
	$('#date').datetimebox("setValue",formatterDate(new Date()));
		setType();	
	})		
/*获取产品类别*/
	function  setType()
	{			
		var com=$('#selType_combobox');
		com.combobox({
			url: 'buyProduct/selProCode',
			valueField:'ids', 
			textField:'name',
			onSelect: function(row) 
				{	
					setCombobox(row.ids);
				} 
		});
	}
//获取产品列表放入下拉
function  setCombobox(typeIds)
{	
	var url='/buyProduct/selProduct';
	var data={"typeIds":typeIds};
	var reData = common_ajax.ajaxFunc(url,data);
	var daJson=$.parseJSON(reData);
	var com=$('#addPro_combobox');
	com.combobox({
		  data:daJson,
		  valueField:'ids', 
		  textField:'probh',
		  filter: function(q, row){ 
		        var opts = $(this).combobox('options'); 
		       // .toUpperCase()小写转大写   toLowerCase()大写转小写
		        return row[opts.textField].toLowerCase().indexOf(q.toLowerCase()) >= 0;//同一转换成小写做比较，这里改成>=即可在任意地方匹配  
		   },
		  formatter: function(row)
		  {		
			  var opts = $(this).combobox('options');
			  row[opts.textField]=row.probh+"("+row.name+")";
			  return    row[opts.textField];
		  },
		  onSelect: function(row) 
			{	
				getProduct(row.ids);
			} 
	});
}
//选中产品编号后查出产品
function getProduct(proIds)
{	
	//获取所有
	var url="/bintoStorage/getProInfo";
	var data={"productIds":proIds};
	var reData = common_ajax.ajaxFunc(url, data,"json");
	reData.intostoragenumber='1';
	var intostoragenumber=parseInt(reData.intostoragenumber);
	var stocknow=parseInt(reData.stocknow);
		reData.total=intostoragenumber+stocknow;
		$('#intoStorage').datagrid('insertRow',{row:reData});
}
/*总库存*/
function total(n)
{	
	var row = $('#intoStorage').datagrid('getSelected');
	var index=$('#intoStorage').datagrid('getRowIndex', row);
	var sum=parseInt(n)+parseInt(row.stocknow);
	if(n==null){
		$('#intoStorage').datagrid('updateRow', {
	        index: index,
	        row: {
	        	intostoragenumber:n,
	        	total:""
	        }
	    });
	}else{
		$('#intoStorage').datagrid('updateRow', {
	        index: index,
	        row: {
	        	intostoragenumber:n,
	        	total:sum
	        }
	    });
	}
	$('#intoStorage').datagrid('beginEdit', index);
}

	/*确认入库*/
	function  save()
	{	
		$('#intoStorage').datagrid("endEdit", editIndex1);
		/*把所有数据都拼接到form表单里*/
		$("#allData").html("");
		var rows = $('#intoStorage').datagrid('getRows');
		for(var i=0;i<rows.length;i++)
		{
			var proids = rows[i].proids;
			var typeids = rows[i].typeids;
			var intostoragenumber = rows[i].intostoragenumber;
			$("#allData").append("<input type='hidden' name='productOutinList["+i+"].productIds' value='"+proids+"'/>");
			$("#allData").append("<input type='hidden' name='productOutinList["+i+"].typeIds' value='"+typeids+"'/>");
			$("#allData").append("<input type='hidden' name='productOutinList["+i+"].number' value='"+intostoragenumber+"'/>");
		}	
		var date=$('#date').val();
		var selType_combobox=$('#selType_combobox').val();
		var addPro_combobox=$('#addPro_combobox').val();
		if(date != "" && selType_combobox != "" && addPro_combobox != "")
		{		
			   var url='/bintoStorage/saveBintoStorage';
				var data=$('#allData_form').serialize();
				var reData = common_ajax.ajaxFunc(url,data);
				if(reData == "200")
				{
					 $.slideMsg("保存成功", parent.$);
					 parent.refresh();
				}else
				{
					$.alert(parent.$, reData);
				}
		}else
		{
			$.alert(parent.$, "信息不能为空，请输入信息！！");
		}
	}
	//得到当前日期
	formatterDate = function (date) {
		var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
		var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
		+ (date.getMonth() + 1);
		var hor = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
		};